
package com.hlkj.pay.app.merchant.ext.impl.gtzfpay.utils;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantAccessCallBackDto;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantBusinessCallBackDto;
import com.hlkj.pay.app.merchant.ext.impl.gtzfpay.dto.GtzfPayConfig;
import com.hlkj.pay.enums.MerchantEnum;

/**
 * @author HlpayTeam
 * @date 2024/10/09 10:02
 */
public class GtzfPayCallBackParamsParseUtil {

    /**
     * 进件回调参数解析
     *
     * @param callBackBody
     * @return
     */
    public static MerchantAccessCallBackDto accessCallBackParamsParse(String callBackBody, GtzfPayConfig config) {
        MerchantAccessCallBackDto merchantAccessCallBackDto = new MerchantAccessCallBackDto();
        JSONObject obj = JSONUtil.parseObj(callBackBody);
        String plainText = GtzfPayRsaUtil.decrypt(obj.getStr("pdata"), config.getMchPublicKey());

        // 参数组装
        boolean sign = GtzfPayRsaUtil.verify(plainText, config.getMchPublicKey());
        if (!sign) {
            merchantAccessCallBackDto.setProcessStatus(false);
            merchantAccessCallBackDto.setRemark("验签失败");
            return merchantAccessCallBackDto;
        }
        JSONObject dataObj = JSONUtil.parseObj(plainText);
        // 参数组装
        merchantAccessCallBackDto.setSubMchId(dataObj.getStr("CUST_ID"));
        String auditStatus = dataObj.getStr("AUTH_RESULT");
        switch (auditStatus) {
            case "00":
                //成功
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGN);
                break;
            default:
                //失败
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                merchantAccessCallBackDto.setRemark(dataObj.getStr("NOT_THROW_REASON"));
                break;
        }
        return merchantAccessCallBackDto;
    }

    /**
     * 业务开通参数解析
     *
     * @param callBackBody
     * @return
     */
    public static MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody, GtzfPayConfig easyPayConfig) {
        MerchantBusinessCallBackDto merchantBusinessCallBackDto = new MerchantBusinessCallBackDto();

        // 参数组装
        JSONObject response = JSONUtil.parseObj(callBackBody);
        JSONObject reqBody = response.getJSONObject("reqBody");
        // 参数组装
        String reqSeqId = reqBody.getStr("applyNo");
        merchantBusinessCallBackDto.setRequestId(reqSeqId);
        String auditStatus = reqBody.getStr("applyNoState");
        switch (auditStatus) {
            case "0":
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGN);
                break;
            case "1":
            case "9":
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                break;
            case "X":
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                merchantBusinessCallBackDto.setRemark("易生审核失败,接口返回信息:" + reqBody.getStr("remark"));
                break;
            default:
                merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                merchantBusinessCallBackDto.setRemark("易生审核失败,接口返回信息: 状态未知");
                break;
        }
        return merchantBusinessCallBackDto;
    }
}
